/*
 * Handle.java
 *
 * Created on 18 maart 2008, 12:01
 *
 * @author wijnand.schepens@hogent.be
 */

package diagram.handles;

import diagram.Draggable;
import diagram.locations.Location;
import diagram.Graphical;
import diagram.Highlightable;
import diagram.Node;
import diagram.locations.AbsoluteLocation;
import diagram.locations.FlexibleRelativeLocation;
import java.awt.Rectangle;
import java.io.Serializable;

/**
 *  a draggable shape that is related to a base-node.
 *  e.g. a resize-handle
 */
public abstract class Handle extends Graphical implements Highlightable, Draggable , Serializable
{
    private Location upperLeft;
    private Location lowerRight;
	private Location location;
    private int ConnectionID;
	protected boolean highlighted = false;
    private boolean selected = false;
    private Graphical base;

    public Handle(){}

	public Handle(Node baseNode, Location location)
	{
		this.base = baseNode;
        attach(baseNode);
        this.location=location;
        this.upperLeft=location;
        setLowerRight(new FlexibleRelativeLocation(upperLeft, 100, 50));
	}

    public Handle(Handle baseHandle, Location location)
	{
		this.base = baseHandle;
        this.location=location;
        this.upperLeft=location;
        setLowerRight(new FlexibleRelativeLocation(upperLeft, 30, 15));
	}


	public boolean isHighlighted()
	{
		return highlighted;
	}

	public void setHighlighted(boolean highlighted)
	{
		this.highlighted = highlighted;
	}

	public boolean isContainedIn(Rectangle rect)
	{
		return rect.contains(getLocation().getX(), getLocation().getY());
	}

        public boolean isSelected()
	{
		return selected;
	}

	public void setSelected(boolean selected)
	{
		this.selected = selected;
	}

   public void attach(Graphical g){
       setBase(g);
    }

    public void detach(){
        setLocation(new AbsoluteLocation(getLocation().getX(), getLocation().getY()));
        setBase(null);
    }
	public void drag(int relX, int relY){
		location.setXY(location.getX() + relX, location.getY() + relY);
	}

	public Location getLocation()
	{
		return location;
	}

	public void setLocation(Location location)
	{
		this.location = location;
	}


    /**
     * @return the ConnectionID
     */
    public int getConnectionID() {
        return ConnectionID;
    }

    /**
     * @param ConnectionID the ConnectionID to set
     */
    public void setConnectionID(int ConnectionID) {
        this.ConnectionID = ConnectionID;
    }

    /**
     * @return the baseNode
     */
    public Graphical getBase() {
        return base;
    }

    /**
     * @param baseNode the baseNode to set
     */
    public void setBase(Graphical base) {
        this.base = base;
    }

    public Location getUpperRight(){
        return new AbsoluteLocation(lowerRight.getX(),upperLeft.getY());
    }

    public Location getLowerLeft(){
        return new AbsoluteLocation(upperLeft.getX(),lowerRight.getY());
    }

    public Location getUpperLeft() {
        return upperLeft;
    }

    public void setUpperLeft(Location upperLeft) {
        this.upperLeft = upperLeft;
    }


    public Location getLowerRight() {
        return lowerRight;
    }

    public void setLowerRight(Location lowerRight) {
        this.lowerRight = lowerRight;
    }

}
